package org.example.bean;

/**
 * @Desc:
 * @Author: yanghongjun
 * @Date: 2025/6/16
 **/
public class ThreadLocalNodePool {
    // 每个线程独立的 NodePool
    private final ThreadLocal<NodePool> localPool;
    // 每个线程池的初始大小
    private final int initialSize;
    // 每个线程池的最大容量
    private final int maxPoolSizePerThread;

    public ThreadLocalNodePool(int initialSize, int maxPoolSizePerThread) {
        this.initialSize = initialSize;
        this.maxPoolSizePerThread = maxPoolSizePerThread;
        localPool = ThreadLocal.withInitial(() -> new NodePool(initialSize, maxPoolSizePerThread));
    }

    /**
     * 获取当前线程专属的 Node
     */
    public Node acquire() {
        return localPool.get().acquire();
    }

    /**
     * 释放当前线程的 Node 回到本地池
     */
    public boolean release(Node node) {
        return localPool.get().release(node);
    }

    /**
     * 获取当前线程池中空闲节点数量（调试或监控用）
     */
    public int getAvailableCount() {
        return localPool.get().getAvailableCount();
    }

}
